from rest_framework import serializers
from .models import User, BankCard, Transaction

# 用户信息序列化器
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["id", "username", "pid", "phone", "address", "date_joined"]

# 银行卡序列化器
class BankCardSerializer(serializers.ModelSerializer):
    # 嵌套用户信息（只读）
    user = UserSerializer(read_only=True)
    # 接收用户 ID（写入时用）
    user_id = serializers.UUIDField(write_only=True)

    class Meta:
        model = BankCard
        fields = ["id", "card_number", "user", "user_id", "balance", "created_at", "is_lost"]

# 交易记录序列化器
class TransactionSerializer(serializers.ModelSerializer):
    # 嵌套银行卡信息（只读）
    card = BankCardSerializer(read_only=True)
    # 接收银行卡 ID（写入时用）
    card_id = serializers.UUIDField(write_only=True)

    class Meta:
        model = Transaction
        fields = ["id", "card", "card_id", "transaction_type", "amount",
                  "target_card", "operator", "status", "created_at", "remark"]